package com.example.mvp.mvppromote.ui.fragment;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.mvp.mvppromote.R;
import com.example.mvp.mvppromote.base.BaseFragment;
import com.example.mvp.mvppromote.model.WeiXinItemBean;
import com.example.mvp.mvppromote.presenter.WeiXinPresenter;
import com.example.mvp.mvppromote.ui.adapter.WeiXinAdapter;
import com.example.mvp.mvppromote.ui.iview.IWeiXinView;
import com.example.mvp.mvppromote.utils.ToaskUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 描述
 * Created by 武彤宇 on 2016/6/27 0027
 */
public class WeiXinFragment extends BaseFragment<WeiXinPresenter> implements IWeiXinView, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.progressBar)
    android.widget.ProgressBar progressBar;
    @BindView(R.id.swipe_target)
    android.support.v7.widget.RecyclerView swipeTarget;
    @BindView(R.id.swipeToLoadLayout)
    android.support.v4.widget.SwipeRefreshLayout swipeToLoadLayout;
    private WeiXinAdapter weiXinAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private int pastVisiblesItems, visibleItemCount, totalItemCount;
    private int currentOffset = 1;
    private ArrayList<WeiXinItemBean> weiXinItemBeans = new ArrayList<WeiXinItemBean>();
    private Context context;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_band;
    }

    @Override
    protected void initData() {
        context = getActivity();
        swipeToLoadLayout.setOnRefreshListener(this);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        swipeTarget.setLayoutManager(mLinearLayoutManager);
        swipeToLoadLayout.setColorSchemeColors(Color.BLUE, Color.GREEN, Color.RED, Color.YELLOW);
        swipeTarget.setHasFixedSize(true);
        // swipeTarget.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        swipeTarget.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    visibleItemCount = mLinearLayoutManager.getChildCount();
                    totalItemCount = mLinearLayoutManager.getItemCount();
                    pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                        onLoadMore();
                    }
                }
            }
        });
        weiXinAdapter = new WeiXinAdapter(weiXinItemBeans, context);
        swipeTarget.setAdapter(weiXinAdapter);
        onRefresh();
    }

    public void onLoadMore() {
        mPresenter.getWeiXin(currentOffset);
    }

    @Override
    public void updateList(ArrayList<WeiXinItemBean> weixinNewses) {
        currentOffset++;
        this.weiXinItemBeans.addAll(weixinNewses);
        weiXinAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected WeiXinPresenter getPresenter() {
        return new WeiXinPresenter(this);
    }

    /*@Override
    public void showDialog() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideDialog() {
        if (swipeToLoadLayout != null) {//不加可能会崩溃
            swipeToLoadLayout.setRefreshing(false);
            loading = false;
            progressBar.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showError(String error) {
        ToaskUtil.toastShort(context, error);
    }*/

    @Override
    public void onRefresh() {
        currentOffset = 1;
        weiXinItemBeans.clear();
        weiXinAdapter.notifyDataSetChanged();
        mPresenter.getWeiXin(currentOffset);
    }

    @Override
    public Context getMyContext() {
        return context;
    }
}
